
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>ScoreCardModel package &#8212; ScoreCardModel  documentation</title>
    <link rel="stylesheet" href="_static/alabaster.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="ScoreCardModel.mixins package" href="ScoreCardModel.mixins.html" />
    <link rel="prev" title="Welcome to ScoreCardModel’s documentation!" href="index.html" />
   
  <link rel="stylesheet" href="_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head>
  <body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="scorecardmodel-package">
<h1>ScoreCardModel package<a class="headerlink" href="#scorecardmodel-package" title="Permalink to this headline">¶</a></h1>
<div class="section" id="module-ScoreCardModel">
<span id="module-contents"></span><h2>Module contents<a class="headerlink" href="#module-ScoreCardModel" title="Permalink to this headline">¶</a></h2>
<div class="section" id="id1">
<h3>评分卡模型<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h3>
<p>所谓评分卡其实只是一种用于表现预测概率的方式,书上说是用于给用户信用打分的.
通常它会结合logistic回归使用.当然了,个人认为用其他分类器也是一样的.</p>
<p>连续特征往往是非线性的,因此预测模型有两种思路</p>
<ul class="simple">
<li><dl class="first docutils">
<dt>先做一些特征工程,通常工业上会将连续数据分段,把它转化为一组离散数据.</dt>
<dd>如何分段也就是这种思路下预测模型准确度的根本了.</dd>
</dl>
</li>
<li>使用深度学习,深度学习自带特征抽象能力.</li>
</ul>
<p>本模型就是第一种思路的实践.</p>
<div class="section" id="id2">
<h4>模块设计:<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h4>
<p>本模块有如下几个子模块:</p>
<ul class="simple">
<li>连续数据离散化模块 discretization</li>
<li>计算离散数据各枚举值的证据权重 weight_of_evidence</li>
<li>分类器模型 models,其中现在只有logistic回归一个实现.</li>
<li>用于计算得分的评分卡模型 score_card</li>
</ul>
</div>
<div class="section" id="id3">
<h4>依赖:<a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h4>
<p>本模块依赖numpy,sklearn,pandas</p>
</div>
<div class="section" id="id4">
<h4>使用方法:<a class="headerlink" href="#id4" title="Permalink to this headline">¶</a></h4>
<p>任何机器学习模型,使用流程都差不多</p>
<ul class="simple">
<li>清洗特征,本模型不管</li>
<li>观察数据,使用`Discretization`来将连续数据离散化.</li>
<li>将离散化后的数据每一列都使用`WeightOfEvidence`来计算离散数据的证据权重,每一项计算出来都可以计算出一个iv值(信息值),都算得了可以比较了挑大的选出来使用.</li>
<li>将离散数据都用`WeightOfEvidence`计算出每项的权重替换这些分类,然后拿这个放入分类器训练</li>
<li>重复上面的步骤找到效果最好的一个模型.</li>
<li>初始化一个`ScoreCardModel`用于计算最终得分</li>
</ul>
</div>
<div class="section" id="id5">
<h4>一个完整的例子:<a class="headerlink" href="#id5" title="Permalink to this headline">¶</a></h4>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sklearn</span> <span class="k">import</span> <span class="n">datasets</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">ScoreCardModel.discretization</span> <span class="k">import</span> <span class="n">Discretization</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">ScoreCardModel.weight_of_evidence</span> <span class="k">import</span> <span class="n">WeightOfEvidence</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">ScoreCardModel.models.logistic_regression_model</span> <span class="k">import</span> <span class="n">LogisticRegressionModel</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">ScoreCardModel.binning.score_card</span> <span class="k">import</span> <span class="n">ScoreCardModel</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">MyLR</span><span class="p">(</span><span class="n">LogisticRegressionModel</span><span class="p">):</span>
<span class="gp">&gt;&gt;&gt; </span>    <span class="k">def</span> <span class="nf">predict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
<span class="gp">&gt;&gt;&gt; </span>         <span class="n">x</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pre_trade</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span>         <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_predict_proba</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span>    <span class="k">def</span> <span class="nf">pre_trade</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="n">result</span> <span class="o">=</span> <span class="p">[]</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">v</span> <span class="ow">in</span> <span class="n">x</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
<span class="gp">&gt;&gt;&gt; </span>            <span class="n">t</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ds</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">transform</span><span class="p">([</span><span class="n">v</span><span class="p">])[</span><span class="mi">0</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span>            <span class="n">r</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">woes</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">transform</span><span class="p">([</span><span class="n">t</span><span class="p">])[</span><span class="mi">0</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span>            <span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">r</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span>    <span class="k">def</span> <span class="nf">_pre_trade_batch_row</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">row</span><span class="p">,</span><span class="n">Y</span><span class="p">,</span><span class="n">bins</span><span class="p">):</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="n">d</span> <span class="o">=</span> <span class="n">Discretization</span><span class="p">(</span><span class="n">bins</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="n">d_row</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">row</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="n">woe</span> <span class="o">=</span> <span class="n">WeightOfEvidence</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="n">woe</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">d_row</span><span class="p">,</span><span class="n">Y</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="k">return</span> <span class="n">d</span><span class="p">,</span><span class="n">woe</span><span class="p">,</span><span class="n">woe</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">d_row</span><span class="p">)</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span>    <span class="k">def</span> <span class="nf">pre_trade_batch</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">X</span><span class="p">,</span><span class="n">Y</span><span class="p">):</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="bp">self</span><span class="o">.</span><span class="n">ds</span> <span class="o">=</span> <span class="p">{}</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="bp">self</span><span class="o">.</span><span class="n">woes</span> <span class="o">=</span> <span class="p">{}</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="bp">self</span><span class="o">.</span><span class="n">table</span> <span class="o">=</span> <span class="p">{}</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="bp">self</span><span class="o">.</span><span class="n">ds</span><span class="p">[</span><span class="s2">&quot;sepal length (cm)&quot;</span><span class="p">],</span><span class="bp">self</span><span class="o">.</span><span class="n">woes</span><span class="p">[</span><span class="s2">&quot;sepal length (cm)&quot;</span><span class="p">],</span><span class="bp">self</span><span class="o">.</span><span class="n">table</span><span class="p">[</span><span class="s2">&quot;sepal length (cm)&quot;</span><span class="p">]</span><span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pre_trade_batch_row</span><span class="p">(</span>
<span class="gp">&gt;&gt;&gt; </span>            <span class="n">X</span><span class="p">[</span><span class="s2">&quot;sepal length (cm)&quot;</span><span class="p">],</span><span class="n">Y</span><span class="p">,[</span><span class="mi">0</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">8</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="bp">self</span><span class="o">.</span><span class="n">ds</span><span class="p">[</span><span class="s1">&#39;sepal width (cm)&#39;</span><span class="p">],</span><span class="bp">self</span><span class="o">.</span><span class="n">woes</span><span class="p">[</span><span class="s1">&#39;sepal width (cm)&#39;</span><span class="p">],</span><span class="bp">self</span><span class="o">.</span><span class="n">table</span><span class="p">[</span><span class="s1">&#39;sepal width (cm)&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pre_trade_batch_row</span><span class="p">(</span>
<span class="gp">&gt;&gt;&gt; </span>            <span class="n">X</span><span class="p">[</span><span class="s1">&#39;sepal width (cm)&#39;</span><span class="p">],</span><span class="n">Y</span><span class="p">,[</span><span class="mi">0</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mf">2.5</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mf">3.5</span><span class="p">,</span><span class="mi">5</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="bp">self</span><span class="o">.</span><span class="n">ds</span><span class="p">[</span><span class="s1">&#39;petal length (cm)&#39;</span><span class="p">],</span><span class="bp">self</span><span class="o">.</span><span class="n">woes</span><span class="p">[</span><span class="s1">&#39;petal length (cm)&#39;</span><span class="p">],</span><span class="bp">self</span><span class="o">.</span><span class="n">table</span><span class="p">[</span><span class="s1">&#39;petal length (cm)&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pre_trade_batch_row</span><span class="p">(</span>
<span class="gp">&gt;&gt;&gt; </span>            <span class="n">X</span><span class="p">[</span><span class="s1">&#39;petal length (cm)&#39;</span><span class="p">],</span><span class="n">Y</span><span class="p">,[</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">7</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="bp">self</span><span class="o">.</span><span class="n">ds</span><span class="p">[</span><span class="s1">&#39;petal width (cm)&#39;</span><span class="p">],</span><span class="bp">self</span><span class="o">.</span><span class="n">woes</span><span class="p">[</span><span class="s1">&#39;petal width (cm)&#39;</span><span class="p">],</span><span class="bp">self</span><span class="o">.</span><span class="n">table</span><span class="p">[</span><span class="s1">&#39;petal width (cm)&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pre_trade_batch_row</span><span class="p">(</span>
<span class="gp">&gt;&gt;&gt; </span>            <span class="n">X</span><span class="p">[</span><span class="s1">&#39;petal width (cm)&#39;</span><span class="p">],</span><span class="n">Y</span><span class="p">,[</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">table</span><span class="p">)</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">iris</span> <span class="o">=</span> <span class="n">datasets</span><span class="o">.</span><span class="n">load_iris</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">iris</span><span class="o">.</span><span class="n">target</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">z</span> <span class="o">=</span> <span class="p">(</span><span class="n">y</span><span class="o">==</span><span class="mi">0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">l</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">iris</span><span class="o">.</span><span class="n">data</span><span class="p">,</span><span class="n">columns</span><span class="o">=</span><span class="n">iris</span><span class="o">.</span><span class="n">feature_names</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">lr</span> <span class="o">=</span> <span class="n">MyLR</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">lr</span><span class="o">.</span><span class="n">train</span><span class="p">(</span><span class="n">l</span><span class="p">,</span><span class="n">z</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">lr</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">l</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">to_dict</span><span class="p">())</span>
<span class="go">array([[ 0.46315882,  0.53684118]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sc</span> <span class="o">=</span> <span class="n">ScoreCardModel</span><span class="p">(</span><span class="n">lr</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sc</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">l</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">to_dict</span><span class="p">())</span>
<span class="go">104.3</span>
</pre></div>
</div>
</div>
<div class="section" id="id6">
<h4>模型序列化<a class="headerlink" href="#id6" title="Permalink to this headline">¶</a></h4>
<p>要实际应用模型我们就得想办法保存我们的训练成果,也就是序列化.这个包使用dill做python的序列化主要工具,
而为了用于传递和存入数据库,我们又使用base64再编码一次.</p>
<p>无论是分类模型还是评分卡模型,都混入了`SerializeMixin`,这个Mixin提供了对象的序列化能力
和从这个序列化后的字符串转化到模型对象的能力.其接口为`obj.dumps`和`clz.loads`</p>
<p>示例:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">sc_str</span> <span class="o">=</span> <span class="n">sc</span><span class="o">.</span><span class="n">dumps</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sc_l</span> <span class="o">=</span> <span class="n">ScoreCardModel</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">sc_str</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sc_l</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">l</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">to_dict</span><span class="p">())</span>
<span class="go">104.3</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="section" id="subpackages">
<h2>Subpackages<a class="headerlink" href="#subpackages" title="Permalink to this headline">¶</a></h2>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference internal" href="ScoreCardModel.mixins.html">ScoreCardModel.mixins package</a><ul>
<li class="toctree-l2"><a class="reference internal" href="ScoreCardModel.mixins.html#submodules">Submodules</a></li>
<li class="toctree-l2"><a class="reference internal" href="ScoreCardModel.mixins.html#module-ScoreCardModel.mixins.serialize_mixin">ScoreCardModel.mixins.serialize_mixin module</a></li>
<li class="toctree-l2"><a class="reference internal" href="ScoreCardModel.mixins.html#module-ScoreCardModel.mixins">Module contents</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="ScoreCardModel.models.html">ScoreCardModel.models package</a><ul>
<li class="toctree-l2"><a class="reference internal" href="ScoreCardModel.models.html#submodules">Submodules</a></li>
<li class="toctree-l2"><a class="reference internal" href="ScoreCardModel.models.html#module-ScoreCardModel.models.logistic_regression_model">ScoreCardModel.models.logistic_regression_model module</a><ul>
<li class="toctree-l3"><a class="reference internal" href="ScoreCardModel.models.html#logistic">logistic回归模型</a><ul>
<li class="toctree-l4"><a class="reference internal" href="ScoreCardModel.models.html#id1">用法</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="ScoreCardModel.models.html#module-ScoreCardModel.models.meta">ScoreCardModel.models.meta module</a></li>
<li class="toctree-l2"><a class="reference internal" href="ScoreCardModel.models.html#module-ScoreCardModel.models">Module contents</a><ul>
<li class="toctree-l3"><a class="reference internal" href="ScoreCardModel.models.html#id2">分类器模型</a></li>
<li class="toctree-l3"><a class="reference internal" href="ScoreCardModel.models.html#id3">logistic回归分类器</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="section" id="submodules">
<h2>Submodules<a class="headerlink" href="#submodules" title="Permalink to this headline">¶</a></h2>
</div>
<div class="section" id="scorecardmodel-discretization-module">
<h2>ScoreCardModel.discretization module<a class="headerlink" href="#scorecardmodel-discretization-module" title="Permalink to this headline">¶</a></h2>
</div>
<div class="section" id="module-ScoreCardModel.score_card">
<span id="scorecardmodel-score-card-module"></span><h2>ScoreCardModel.score_card module<a class="headerlink" href="#module-ScoreCardModel.score_card" title="Permalink to this headline">¶</a></h2>
<div class="section" id="id7">
<h3>评分卡<a class="headerlink" href="#id7" title="Permalink to this headline">¶</a></h3>
<p>用于二分类问题通过模型预测的概率来计算得分</p>
<div class="section" id="id8">
<h4>计算公式:<a class="headerlink" href="#id8" title="Permalink to this headline">¶</a></h4>
<div class="math">
\[factor = {\frac {p} {log(2)}}\]</div>
<div class="math">
\[offset = b - p \cdot {\frac {log(o)} {log(2)}}\]</div>
<div class="math">
\[odds = {\frac {p_t} {p_f}}\]</div>
<div class="math">
\[score = factor \cdot {log(odds)} + offset\]</div>
</div>
<div class="section" id="id9">
<h4>使用方法:<a class="headerlink" href="#id9" title="Permalink to this headline">¶</a></h4>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">sc</span> <span class="o">=</span> <span class="n">ScoreCardModel</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sc</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">sc</span><span class="o">.</span><span class="n">pre_trade</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
</pre></div>
</div>
<p>评分卡类默认会使用包装的分类器的`predict`和`pre_trade`方法,
我们也可以适当的重写评分卡的这两个方法来满足业务要求.</p>
</div>
<div class="section" id="ks">
<h4>KS 曲线:<a class="headerlink" href="#ks" title="Permalink to this headline">¶</a></h4>
<p>不知为何,搞经济金融的喜欢用KS曲线来评估评分卡的效果.abs</p>
<p>所谓KS曲线计算方法很简单:</p>
<ol class="arabic simple">
<li>将得分与实际标签合并后以得分从大到小排序,这个序列设为total</li>
<li>计算出总共标签中的好标签数量和坏标签数量good_total,bad_total</li>
<li>获取total前i%的用户计算其中好用户数量good和坏用户数量bad并计算(good/good_total-bad/bad_total)的绝对值,这个值就是i%位的ks值,i从0计算到100,这样得到i%和每位对应的ks值就可以用于绘制x轴和y轴.</li>
<li>将这两个序列画出来也就得到了ks曲线图.</li>
</ol>
<p>在这个计算过程中另外有意义的几个值为:</p>
<p>好坏比 good/bad</p>
<p>好占比 good/good_total</p>
<p>坏占比 bad/bad_total</p>
<dl class="class">
<dt id="ScoreCardModel.score_card.KS">
<em class="property">class </em><code class="descclassname">ScoreCardModel.score_card.</code><code class="descname">KS</code><span class="sig-paren">(</span><em>ks</em>, <em>good_bad_rate</em>, <em>bad_rate</em>, <em>good_rate</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/ScoreCardModel/score_card.html#KS"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#ScoreCardModel.score_card.KS" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <code class="xref py py-class docutils literal"><span class="pre">object</span></code></p>
</dd></dl>

<dl class="class">
<dt id="ScoreCardModel.score_card.ScoreCardModel">
<em class="property">class </em><code class="descclassname">ScoreCardModel.score_card.</code><code class="descname">ScoreCardModel</code><span class="sig-paren">(</span><em>model</em>, <em>b=100</em>, <em>o=1</em>, <em>p=20</em>, <em>round_=1</em>, <em>threshold=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/ScoreCardModel/score_card.html#ScoreCardModel"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardModel" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <a class="reference internal" href="ScoreCardModel.mixins.html#ScoreCardModel.mixins.serialize_mixin.SerializeMixin" title="ScoreCardModel.mixins.serialize_mixin.SerializeMixin"><code class="xref py py-class docutils literal"><span class="pre">ScoreCardModel.mixins.serialize_mixin.SerializeMixin</span></code></a></p>
<p>本模型需要使用一个已经训练好的分类器来初始化,预测,计算评分也都是依赖于它.</p>
<dl class="attribute">
<dt id="ScoreCardModel.score_card.ScoreCardModel._model">
<code class="descname">_model</code><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardModel._model" title="Permalink to this definition">¶</a></dt>
<dd><p><em>ScoreCradModel.models.meta</em> –</p>
<ul class="simple">
<li>训练好的预测模型</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt id="ScoreCardModel.score_card.ScoreCardModel.b">
<code class="descname">b</code><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardModel.b" title="Permalink to this definition">¶</a></dt>
<dd><p><em>int</em> –</p>
<ul class="simple">
<li>偏置量的常数项,用于作为基数</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt id="ScoreCardModel.score_card.ScoreCardModel.o">
<code class="descname">o</code><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardModel.o" title="Permalink to this definition">¶</a></dt>
<dd><p><em>int</em> –</p>
<ul class="simple">
<li>用于计算偏置量</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt id="ScoreCardModel.score_card.ScoreCardModel.p">
<code class="descname">p</code><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardModel.p" title="Permalink to this definition">¶</a></dt>
<dd><p><em>int</em> –</p>
<ul class="simple">
<li>用于计算偏置量和因数项</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt id="ScoreCardModel.score_card.ScoreCardModel.round_">
<code class="descname">round_</code><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardModel.round_" title="Permalink to this definition">¶</a></dt>
<dd><p><em>int</em> –</p>
<ul class="simple">
<li>精度</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt id="ScoreCardModel.score_card.ScoreCardModel.threshold">
<code class="descname">threshold</code><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardModel.threshold" title="Permalink to this definition">¶</a></dt>
<dd><p><em>float</em> –</p>
<ul class="simple">
<li>阈值,可选</li>
</ul>
</dd></dl>

<dl class="method">
<dt id="ScoreCardModel.score_card.ScoreCardModel.pre_trade">
<code class="descname">pre_trade</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/ScoreCardModel/score_card.html#ScoreCardModel.pre_trade"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardModel.pre_trade" title="Permalink to this definition">¶</a></dt>
<dd><p>“数据预处理,预测的时候由于输入未必是处理好的,因此需要先做下预处理</p>
</dd></dl>

<dl class="method">
<dt id="ScoreCardModel.score_card.ScoreCardModel.predict">
<code class="descname">predict</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/ScoreCardModel/score_card.html#ScoreCardModel.predict"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardModel.predict" title="Permalink to this definition">¶</a></dt>
<dd><p>用于预测某一条预处理过的特征向量得分的方法</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>x</strong> (<em>Sequence</em>) – <ul class="simple">
<li>用于分段的序列</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><ul class="simple">
<li>预测出来的分数</li>
</ul>
<p>bool: - 预测的分数超过阈值则返回True,否则False</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body">float</td>
</tr>
</tbody>
</table>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="ScoreCardModel.score_card.ScoreCardWithKSModel">
<em class="property">class </em><code class="descclassname">ScoreCardModel.score_card.</code><code class="descname">ScoreCardWithKSModel</code><span class="sig-paren">(</span><em>model</em>, <em>b=100</em>, <em>o=1</em>, <em>p=20</em>, <em>round_=1</em>, <em>threshold=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/ScoreCardModel/score_card.html#ScoreCardWithKSModel"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardWithKSModel" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <a class="reference internal" href="#ScoreCardModel.score_card.ScoreCardModel" title="ScoreCardModel.score_card.ScoreCardModel"><code class="xref py py-class docutils literal"><span class="pre">ScoreCardModel.score_card.ScoreCardModel</span></code></a></p>
<p>本模型需要使用一个已经训练好的分类器来初始化.预测,计算评分也都是依赖于它.
它是ScoreCardModel的子类,并且可以用ScoreCardModel或者它的子类实例化.
这个类主要是新增了一些类方法.之所以弄个子类,是为了序列化的时候不用引入matplotlib作为依赖.
这个类更加合适离线使用,最好别用来序列化.</p>
<dl class="attribute">
<dt id="ScoreCardModel.score_card.ScoreCardWithKSModel._model">
<code class="descname">_model</code><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardWithKSModel._model" title="Permalink to this definition">¶</a></dt>
<dd><p><em>ScoreCradModel.models.meta</em> –</p>
<ul class="simple">
<li>训练好的预测模型</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt id="ScoreCardModel.score_card.ScoreCardWithKSModel.b">
<code class="descname">b</code><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardWithKSModel.b" title="Permalink to this definition">¶</a></dt>
<dd><p><em>int</em> –</p>
<ul class="simple">
<li>偏置量的常数项,用于作为基数</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt id="ScoreCardModel.score_card.ScoreCardWithKSModel.o">
<code class="descname">o</code><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardWithKSModel.o" title="Permalink to this definition">¶</a></dt>
<dd><p><em>int</em> –</p>
<ul class="simple">
<li>用于计算偏置量</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt id="ScoreCardModel.score_card.ScoreCardWithKSModel.p">
<code class="descname">p</code><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardWithKSModel.p" title="Permalink to this definition">¶</a></dt>
<dd><p><em>int</em> –</p>
<ul class="simple">
<li>用于计算偏置量和因数项</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt id="ScoreCardModel.score_card.ScoreCardWithKSModel.round_">
<code class="descname">round_</code><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardWithKSModel.round_" title="Permalink to this definition">¶</a></dt>
<dd><p><em>int</em> –</p>
<ul class="simple">
<li>精度</li>
</ul>
</dd></dl>

<dl class="classmethod">
<dt id="ScoreCardModel.score_card.ScoreCardWithKSModel.Drawks">
<em class="property">classmethod </em><code class="descname">Drawks</code><span class="sig-paren">(</span><em>X_score</em>, <em>y</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/ScoreCardModel/score_card.html#ScoreCardWithKSModel.Drawks"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardWithKSModel.Drawks" title="Permalink to this definition">¶</a></dt>
<dd><p>绘制ks曲线</p>
<dl class="attribute">
<dt id="ScoreCardModel.score_card.ScoreCardWithKSModel.X_score">
<code class="descname">X_score</code><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardWithKSModel.X_score" title="Permalink to this definition">¶</a></dt>
<dd><p><em>Sequence[number]</em> –</p>
<ul class="simple">
<li>预测的得分</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt id="ScoreCardModel.score_card.ScoreCardWithKSModel.y">
<code class="descname">y</code><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardWithKSModel.y" title="Permalink to this definition">¶</a></dt>
<dd><p><em>Sequence[number]</em> –</p>
<ul class="simple">
<li>实际的标签</li>
</ul>
</dd></dl>

</dd></dl>

<dl class="classmethod">
<dt id="ScoreCardModel.score_card.ScoreCardWithKSModel.From_scorecard">
<em class="property">classmethod </em><code class="descname">From_scorecard</code><span class="sig-paren">(</span><em>obj</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/ScoreCardModel/score_card.html#ScoreCardWithKSModel.From_scorecard"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardWithKSModel.From_scorecard" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="classmethod">
<dt id="ScoreCardModel.score_card.ScoreCardWithKSModel.Get_ks">
<em class="property">classmethod </em><code class="descname">Get_ks</code><span class="sig-paren">(</span><em>X_score</em>, <em>y</em>, <em>*</em>, <em>threshold=0.1</em>, <em>threshold_score=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/ScoreCardModel/score_card.html#ScoreCardWithKSModel.Get_ks"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardWithKSModel.Get_ks" title="Permalink to this definition">¶</a></dt>
<dd><p>计算在某处时的ks值,可以是阈值百分位数,也可以是某个分数</p>
<dl class="attribute">
<dt>
<code class="descname">X_score</code></dt>
<dd><p><em>Sequence[number]</em> –</p>
<ul class="simple">
<li>预测的得分</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">y</code></dt>
<dd><p><em>Sequence[number]</em> –</p>
<ul class="simple">
<li>实际的标签</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt id="ScoreCardModel.score_card.ScoreCardWithKSModel.threshold">
<code class="descname">threshold</code><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardWithKSModel.threshold" title="Permalink to this definition">¶</a></dt>
<dd><p><em>float</em> –</p>
<ul class="simple">
<li>阈值百分位数,默认为0.1也就是前10%</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt id="ScoreCardModel.score_card.ScoreCardWithKSModel.threshold_score">
<code class="descname">threshold_score</code><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardWithKSModel.threshold_score" title="Permalink to this definition">¶</a></dt>
<dd><p><em>number</em> –</p>
<ul class="simple">
<li>位置阈值分,取在它所在排序位置之前(包含这个分数的位置)的计算ks值</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt id="ScoreCardModel.score_card.ScoreCardWithKSModel.score">
<code class="descname">score</code><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardWithKSModel.score" title="Permalink to this definition">¶</a></dt>
<dd><p><em>number</em> –</p>
<ul class="simple">
<li>阈值分,大于等于它的是True,否贼是False</li>
</ul>
</dd></dl>

</dd></dl>

<dl class="classmethod">
<dt id="ScoreCardModel.score_card.ScoreCardWithKSModel.Score_to_threshold">
<em class="property">classmethod </em><code class="descname">Score_to_threshold</code><span class="sig-paren">(</span><em>X_score</em>, <em>*</em>, <em>y=None</em>, <em>score=100</em>, <em>round_=4</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/ScoreCardModel/score_card.html#ScoreCardWithKSModel.Score_to_threshold"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardWithKSModel.Score_to_threshold" title="Permalink to this definition">¶</a></dt>
<dd><p>可以通过得分获取其在训练数据总体上的百分位数,并打印以此为阈值时的预测效果.
也可以通过得分获取其在某一个数据集上的百分位数.</p>
<dl class="attribute">
<dt>
<code class="descname">X_score</code></dt>
<dd><p><em>Sequence[number]</em> –</p>
<ul class="simple">
<li>预测的得分</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">y</code></dt>
<dd><p><em>Sequence[number]</em> –</p>
<ul class="simple">
<li>实际的标签,可以为空</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">score</code></dt>
<dd><p><em>number</em> –</p>
<ul class="simple">
<li>阈值分,大于它的为T,否则为F</li>
</ul>
</dd></dl>

</dd></dl>

<dl class="classmethod">
<dt id="ScoreCardModel.score_card.ScoreCardWithKSModel.Threshold_to_score">
<em class="property">classmethod </em><code class="descname">Threshold_to_score</code><span class="sig-paren">(</span><em>X_score</em>, <em>threshold=0.2</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/ScoreCardModel/score_card.html#ScoreCardWithKSModel.Threshold_to_score"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardWithKSModel.Threshold_to_score" title="Permalink to this definition">¶</a></dt>
<dd><p>可以通过得分获取其在训练数据总体上的百分位数,并打印以此为阈值时的预测效果.
也可以通过得分获取其在某一个数据集上的百分位数.</p>
<dl class="attribute">
<dt>
<code class="descname">X_score</code></dt>
<dd><p><em>Sequence[number]</em> –</p>
<ul class="simple">
<li>预测的得分</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">threshold</code></dt>
<dd><p><em>float</em> –</p>
<ul class="simple">
<li>阈值百分位数,0~1之间,按从大到小的顺序取值,0.1代表从大到小前10%</li>
</ul>
</dd></dl>

</dd></dl>

<dl class="method">
<dt id="ScoreCardModel.score_card.ScoreCardWithKSModel.drawks">
<code class="descname">drawks</code><span class="sig-paren">(</span><em>X</em>, <em>y</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/ScoreCardModel/score_card.html#ScoreCardWithKSModel.drawks"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardWithKSModel.drawks" title="Permalink to this definition">¶</a></dt>
<dd><p>绘制ks曲线</p>
<dl class="attribute">
<dt id="ScoreCardModel.score_card.ScoreCardWithKSModel.X">
<code class="descname">X</code><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardWithKSModel.X" title="Permalink to this definition">¶</a></dt>
<dd><p><em>pd.DateFrame</em> –</p>
<ul class="simple">
<li>待预测特征数据</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">y</code></dt>
<dd><p><em>Sequence[number]</em> –</p>
<ul class="simple">
<li>实际的标签</li>
</ul>
</dd></dl>

</dd></dl>

<dl class="method">
<dt id="ScoreCardModel.score_card.ScoreCardWithKSModel.get_ks">
<code class="descname">get_ks</code><span class="sig-paren">(</span><em>X</em>, <em>y</em>, <em>*</em>, <em>threshold=0.1</em>, <em>threshold_score=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/ScoreCardModel/score_card.html#ScoreCardWithKSModel.get_ks"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardWithKSModel.get_ks" title="Permalink to this definition">¶</a></dt>
<dd><p>计算在某处时的ks值,可以是阈值百分位数,也可以是某个分数</p>
<dl class="attribute">
<dt>
<code class="descname">X</code></dt>
<dd><p><em>pd.DateFrame</em> –</p>
<ul class="simple">
<li>待预测特征数据</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">y</code></dt>
<dd><p><em>Sequence[number]</em> –</p>
<ul class="simple">
<li>实际的标签</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">threshold</code></dt>
<dd><p><em>float</em> –</p>
<ul class="simple">
<li>阈值百分位数,默认为0.1也就是前10%</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">threshold_score</code></dt>
<dd><p><em>number</em> –</p>
<ul class="simple">
<li>位置阈值分,取在它所在排序位置之前(包含这个分数的位置)的计算ks值</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">score</code></dt>
<dd><p><em>number</em> –</p>
<ul class="simple">
<li>阈值分,大于等于它的是True,否贼是False</li>
</ul>
</dd></dl>

</dd></dl>

<dl class="method">
<dt id="ScoreCardModel.score_card.ScoreCardWithKSModel.score_to_threshold">
<code class="descname">score_to_threshold</code><span class="sig-paren">(</span><em>X</em>, <em>*</em>, <em>y=None</em>, <em>score=100</em>, <em>round_=4</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/ScoreCardModel/score_card.html#ScoreCardWithKSModel.score_to_threshold"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardWithKSModel.score_to_threshold" title="Permalink to this definition">¶</a></dt>
<dd><p>可以通过得分获取其在训练数据总体上的百分位数,并打印以此为阈值时的预测效果.
也可以通过得分获取其在某一个数据集上的百分位数.</p>
<dl class="attribute">
<dt>
<code class="descname">X</code></dt>
<dd><p><em>pd.DateFrame</em> –</p>
<ul class="simple">
<li>待预测特征数据</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">y</code></dt>
<dd><p><em>Sequence[number]</em> –</p>
<ul class="simple">
<li>实际的标签,可以为空</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">score</code></dt>
<dd><p><em>number</em> –</p>
<ul class="simple">
<li>阈值分,大于它的为T,否则为F</li>
</ul>
</dd></dl>

</dd></dl>

<dl class="method">
<dt id="ScoreCardModel.score_card.ScoreCardWithKSModel.threshold_to_score">
<code class="descname">threshold_to_score</code><span class="sig-paren">(</span><em>X</em>, <em>threshold=0.2</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/ScoreCardModel/score_card.html#ScoreCardWithKSModel.threshold_to_score"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#ScoreCardModel.score_card.ScoreCardWithKSModel.threshold_to_score" title="Permalink to this definition">¶</a></dt>
<dd><p>可以通过得分获取其在训练数据总体上的百分位数,并打印以此为阈值时的预测效果.
也可以通过得分获取其在某一个数据集上的百分位数.</p>
<dl class="attribute">
<dt>
<code class="descname">X</code></dt>
<dd><p><em>pd.DateFrame</em> –</p>
<ul class="simple">
<li>待预测特征数据</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt>
<code class="descname">threshold</code></dt>
<dd><p><em>float</em> –</p>
<ul class="simple">
<li>阈值百分位数,0~1之间,按从大到小的顺序取值,0.1代表从大到小前10%</li>
</ul>
</dd></dl>

</dd></dl>

</dd></dl>

</div>
</div>
</div>
<div class="section" id="module-ScoreCardModel.weight_of_evidence">
<span id="scorecardmodel-weight-of-evidence-module"></span><h2>ScoreCardModel.weight_of_evidence module<a class="headerlink" href="#module-ScoreCardModel.weight_of_evidence" title="Permalink to this headline">¶</a></h2>
<div class="section" id="woe">
<h3>计算Woe<a class="headerlink" href="#woe" title="Permalink to this headline">¶</a></h3>
<p>针对离散数据,我们需要将离散的枚举值替换成数值才可以用于计算.这些数值就是各个枚举值的权重.
广义上讲,woe可以算是一种编码方式.
如何计算这些权重呢?这就得训练.我们需要一组二值代表签数据,
通过统计离散特征不同枚举值对目标数据的响应情况来计算触发概率,</p>
<div class="section" id="id10">
<h4>触发概率<a class="headerlink" href="#id10" title="Permalink to this headline">¶</a></h4>
<p>本模型针对二分类问题,事件也就是只有True,False两种.我们当然认为True,枚举值i的触发概率可以这样计算,
比如某个枚举值i对true的触发概率,就是所有i值时是true的数量除以总的t的数量</p>
<div class="math">
\[p_f = {\frac {f_i} {f_{total}}}\]</div>
<div class="math">
\[p_t = {\frac {t_i} {t_{total}}}\]</div>
</div>
<div class="section" id="id11">
<h4>woe<a class="headerlink" href="#id11" title="Permalink to this headline">¶</a></h4>
<p>woe就是计算正负概率的信息值</p>
<div class="math">
\[woe_i = log_2({\frac {p_f} {p_t}})\]</div>
</div>
<div class="section" id="iv">
<h4>iv<a class="headerlink" href="#iv" title="Permalink to this headline">¶</a></h4>
<p>iv值就是这以特征的总信息量也就是各枚举值信息量的和</p>
<div class="math">
\[IV_i = (p_f-p_t)*log_2({\frac {p_f} {p_t}})\]</div>
<div class="math">
\[IV = \sum_{k=0}^n IV_i\]</div>
</div>
<div class="section" id="id12">
<h4>使用方法:<a class="headerlink" href="#id12" title="Permalink to this headline">¶</a></h4>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sklearn</span> <span class="k">import</span> <span class="n">datasets</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">iris</span> <span class="o">=</span> <span class="n">datasets</span><span class="o">.</span><span class="n">load_iris</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">iris</span><span class="o">.</span><span class="n">target</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">z</span> <span class="o">=</span> <span class="p">(</span><span class="n">y</span><span class="o">==</span><span class="mi">0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">l</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">iris</span><span class="o">.</span><span class="n">data</span><span class="p">,</span><span class="n">columns</span><span class="o">=</span><span class="n">iris</span><span class="o">.</span><span class="n">feature_names</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">Discretization</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span><span class="mf">5.0</span><span class="p">,</span><span class="mf">5.5</span><span class="p">,</span><span class="mi">7</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">re</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">l</span><span class="p">[</span><span class="s2">&quot;sepal length (cm)&quot;</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">woe</span> <span class="o">=</span> <span class="n">WeightOfEvidence</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">woe</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">re</span><span class="p">,</span><span class="n">z</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">woe</span><span class="o">.</span><span class="n">woe</span>
<span class="go">{&#39;(0, 5]&#39;: 2.6390573296152589,</span>
<span class="go"> &#39;(5, 5.5]&#39;: 1.5581446180465499,</span>
<span class="go"> &#39;(5.5, 7]&#39;: -2.5389738710582761}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">woe</span><span class="o">.</span><span class="n">iv</span>
<span class="go">3.617034906554693</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">test</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;(0, 5]&#39;</span><span class="p">,</span> <span class="s1">&#39;(0, 5]&#39;</span><span class="p">,</span> <span class="s1">&#39;(5.5, 7]&#39;</span><span class="p">,</span> <span class="s1">&#39;(5.5, 7]&#39;</span><span class="p">,</span> <span class="s1">&#39;(5, 5.5]&#39;</span><span class="p">,</span> <span class="s1">&#39;(5, 5.5]&#39;</span><span class="p">,</span>
<span class="go">       &#39;(5.5, 7]&#39;, &#39;(5, 5.5]&#39;, &#39;(5, 5.5]&#39;, &#39;(5, 5.5]&#39;, &#39;(0, 5]&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">woe</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">test</span><span class="p">)</span>
<span class="go">array([ 2.63905733,  2.63905733, -2.53897387, -2.53897387,  1.55814462,</span>
<span class="go">        1.55814462, -2.53897387,  1.55814462,  1.55814462,  1.55814462,</span>
<span class="go">        2.63905733])</span>
</pre></div>
</div>
<dl class="class">
<dt id="ScoreCardModel.weight_of_evidence.WeightOfEvidence">
<em class="property">class </em><code class="descclassname">ScoreCardModel.weight_of_evidence.</code><code class="descname">WeightOfEvidence</code><a class="reference internal" href="_modules/ScoreCardModel/weight_of_evidence.html#WeightOfEvidence"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#ScoreCardModel.weight_of_evidence.WeightOfEvidence" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <code class="xref py py-class docutils literal"><span class="pre">object</span></code></p>
<p>计算某一离散特征的woe值
.. attribute:: woe</p>
<blockquote>
<div><p><em>Dict</em> –</p>
<ul class="simple">
<li>训练好的证据权重</li>
</ul>
</div></blockquote>
<dl class="attribute">
<dt id="ScoreCardModel.weight_of_evidence.WeightOfEvidence.iv">
<code class="descname">iv</code><a class="headerlink" href="#ScoreCardModel.weight_of_evidence.WeightOfEvidence.iv" title="Permalink to this definition">¶</a></dt>
<dd><p><em>Float</em> –</p>
<ul class="simple">
<li>训练的离散特征的信息量</li>
</ul>
</dd></dl>

<dl class="method">
<dt id="ScoreCardModel.weight_of_evidence.WeightOfEvidence.fit">
<code class="descname">fit</code><span class="sig-paren">(</span><em>x</em>, <em>y</em>, <em>*</em>, <em>event=1</em>, <em>woe_min=-20</em>, <em>woe_max=20</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/ScoreCardModel/weight_of_evidence.html#WeightOfEvidence.fit"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#ScoreCardModel.weight_of_evidence.WeightOfEvidence.fit" title="Permalink to this definition">¶</a></dt>
<dd><p>训练对单独一项自变量(列,特征)的woe值.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>x</strong> (<em>Sequence</em>) – <ul>
<li>离散特征序列</li>
</ul>
</li>
<li><strong>y</strong> (<em>Sequence</em>) – <ul>
<li>用于训练的标签序列</li>
</ul>
</li>
<li><strong>event</strong> (<em>any</em>) – <ul>
<li>True指代的触发事件</li>
</ul>
</li>
<li><strong>woe_min</strong> (<em>munber</em>) – <ul>
<li>woe的最小值,默认值为-20</li>
</ul>
</li>
<li><strong>woe_max</strong> (<em>munber</em>) – <ul>
<li>woe的最大值,默认值为20</li>
</ul>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="ScoreCardModel.weight_of_evidence.WeightOfEvidence.transform">
<code class="descname">transform</code><span class="sig-paren">(</span><em>X</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/ScoreCardModel/weight_of_evidence.html#WeightOfEvidence.transform"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#ScoreCardModel.weight_of_evidence.WeightOfEvidence.transform" title="Permalink to this definition">¶</a></dt>
<dd><p>将离散特征序列转换为woe值组成的序列</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>X</strong> (<em>Sequence</em>) – <ul class="simple">
<li>离散特征序列</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><ul class="simple">
<li>替换特征序列枚举值为woe对应数值后的序列</li>
</ul>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body">numpy.array</td>
</tr>
</tbody>
</table>
</dd></dl>

</dd></dl>

</div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">ScoreCardModel package</a><ul>
<li><a class="reference internal" href="#module-ScoreCardModel">Module contents</a><ul>
<li><a class="reference internal" href="#id1">评分卡模型</a><ul>
<li><a class="reference internal" href="#id2">模块设计:</a></li>
<li><a class="reference internal" href="#id3">依赖:</a></li>
<li><a class="reference internal" href="#id4">使用方法:</a></li>
<li><a class="reference internal" href="#id5">一个完整的例子:</a></li>
<li><a class="reference internal" href="#id6">模型序列化</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#subpackages">Subpackages</a></li>
<li><a class="reference internal" href="#submodules">Submodules</a></li>
<li><a class="reference internal" href="#scorecardmodel-discretization-module">ScoreCardModel.discretization module</a></li>
<li><a class="reference internal" href="#module-ScoreCardModel.score_card">ScoreCardModel.score_card module</a><ul>
<li><a class="reference internal" href="#id7">评分卡</a><ul>
<li><a class="reference internal" href="#id8">计算公式:</a></li>
<li><a class="reference internal" href="#id9">使用方法:</a></li>
<li><a class="reference internal" href="#ks">KS 曲线:</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#module-ScoreCardModel.weight_of_evidence">ScoreCardModel.weight_of_evidence module</a><ul>
<li><a class="reference internal" href="#woe">计算Woe</a><ul>
<li><a class="reference internal" href="#id10">触发概率</a></li>
<li><a class="reference internal" href="#id11">woe</a></li>
<li><a class="reference internal" href="#iv">iv</a></li>
<li><a class="reference internal" href="#id12">使用方法:</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="index.html">Documentation overview</a><ul>
      <li>Previous: <a href="index.html" title="previous chapter">Welcome to ScoreCardModel’s documentation!</a></li>
      <li>Next: <a href="ScoreCardModel.mixins.html" title="next chapter">ScoreCardModel.mixins package</a></li>
  </ul></li>
</ul>
</div>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/ScoreCardModel.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2017, 87.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 1.6.5</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.10</a>
      
      |
      <a href="_sources/ScoreCardModel.rst.txt"
          rel="nofollow">Page source</a>
    </div>

    

    
  </body>
</html>